﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace YangtzeFilterTestSystem
{
    public partial class FilterTestParametersForm : Form
    {

        XmlDocument doc = new XmlDocument();  //初始化和保存本Form里面文本框的一些信息
        
        public FilterTestParametersForm()
        {
            InitializeComponent();

            RatedFlowrateTBox.Text = GlobalVariables.RatedFlowrate.ToString();



            OverallPenetrationTBox.Text = (Filter.OverallPenetration*100).ToString()+"%";
            LocalPenetrationTBox.Text = (Filter.LocalPenetration*100).ToString()+"%";

            doc.Load(GlobalVariables.RootDirectory + "ApplicationData\\AppConfig.xml");

            Filter.Type = (doc.GetElementsByTagName("FilterType")).Item(0).InnerText;
            FilterTypeCBox.SelectedItem = Filter.Type;


            OverallPenetrationTBox.Text = (Filter.OverallPenetration * 100).ToString() + "%";//解决再次开启程序时，局部透过率和总体透过率不变问题2012.04.23
            LocalPenetrationTBox.Text = (Filter.LocalPenetration * 100).ToString() + "%";//解决再次开启程序时，局部透过率和总体透过率不变问题2012.04.23

            
            MPPSCBox.SelectedItem = (doc.GetElementsByTagName("MPPS")).Item(0).InnerText;
            GlobalVariables.strMPPS = (string)MPPSCBox.SelectedItem;

            FilterTypeCBox.SelectedValueChanged += new EventHandler(FilterTypeCBox_SelectedValueChanged);

            MPPSCBox.SelectedValueChanged += new EventHandler(MPPSCBox_SelectedValueChanged);

            FrameFilterLengthTBox.Text = (doc.GetElementsByTagName("FilterLength")).Item(0).InnerText;
            FrameFilterWidthTBox.Text = (doc.GetElementsByTagName("FilterWidth")).Item(0).InnerText;
            FrameFilterHeightTBox.Text = (doc.GetElementsByTagName("FilterHeight")).Item(0).InnerText;

            FoldingFilterLengthTBox.Text = (doc.GetElementsByTagName("FoldingLength")).Item(0).InnerText;
            FoldingFilterWidthTBox.Text = (doc.GetElementsByTagName("FoldingWidth")).Item(0).InnerText;


            UpstreamConcentrationTBox.Text = (doc.GetElementsByTagName("UpstreamConcentration")).Item(0).InnerText;
            DownstreamOverallConcentrationTBox.Text = (Filter.OverallPenetration * Convert.ToDouble(UpstreamConcentrationTBox.Text)).ToString();
            DownstreamLocalConcentrationTBox.Text = (Filter.LocalPenetration * Convert.ToDouble(UpstreamConcentrationTBox.Text)).ToString();

            DilutionFactorTBox.Text = (doc.GetElementsByTagName("DilutionFactor")).Item(0).InnerText;

            LossFactorKbTBox.Text = (doc.GetElementsByTagName("LossFactorKb")).Item(0).InnerText;
            GlobalVariables.kb = Convert.ToDouble(LossFactorKbTBox.Text);

            ProbeDimensionsTBox.Text = "34mm Diameter";

            Probe_apTBox.Text = (doc.GetElementsByTagName("Probe_ap")).Item(0).InnerText;
            Probe_bpTBox.Text = (doc.GetElementsByTagName("Probe_bp")).Item(0).InnerText;
            ScanningVelocityTBox.Text = (doc.GetElementsByTagName("SettingVelocity")).Item(0).InnerText;


        }

        void FilterTypeCBox_SelectedValueChanged(object sender, EventArgs e)
        {
            Filter.Type = FilterTypeCBox.SelectedItem.ToString(); ;

            OverallPenetrationTBox.Text = (Filter.OverallPenetration * 100).ToString() + "%";
            LocalPenetrationTBox.Text = (Filter.LocalPenetration * 100).ToString() + "%";

            DownstreamOverallConcentrationTBox.Text = (Filter.OverallPenetration * Convert.ToDouble(UpstreamConcentrationTBox.Text)).ToString("N2");
            DownstreamLocalConcentrationTBox.Text = (Filter.LocalPenetration * Convert.ToDouble(UpstreamConcentrationTBox.Text)).ToString("N2");
        }

        void MPPSCBox_SelectedValueChanged(object sender, EventArgs e)
        {
            GlobalVariables.strMPPS = MPPSCBox.SelectedItem.ToString();
        }

        private void CalculateBtn_Click(object sender, EventArgs e)
        {
            try
            {
                Filter.Type = FilterTypeCBox.SelectedItem.ToString();
                GlobalVariables.strMPPS = MPPSCBox.SelectedItem.ToString();

                Filter.Length = Convert.ToUInt32(FrameFilterLengthTBox.Text);
                Filter.Width = Convert.ToUInt32(FrameFilterWidthTBox.Text);
                Filter.Height = Convert.ToUInt32(FrameFilterHeightTBox.Text);

                Filter.FoldingLength = Convert.ToUInt32(FoldingFilterLengthTBox.Text);
                Filter.FoldingWidth = Convert.ToUInt32(FoldingFilterWidthTBox.Text);

                GlobalVariables.DilutionRatio = 1.0 / Convert.ToDouble(DilutionFactorTBox.Text);

                Probe.ap = Convert.ToDouble(Probe_apTBox.Text);
                Probe.bp = Convert.ToDouble(Probe_bpTBox.Text);
                Probe.SettingVelocity = Convert.ToDouble(ScanningVelocityTBox.Text);

                PassageVelocityTBox.Text = (GlobalVariables.RatedFlowrate / (Filter.FoldingLength * Filter.FoldingWidth) * 1000 * 1000 / 3600).ToString();

                CountIntervalTBox.Text = "1";//(Probe.ap / Probe.SettingVelocity).ToString()

                AnalysisVolumeTBox.Text = (Convert.ToDouble(CountIntervalTBox.Text) * 28.3 / 60 * 1000).ToString();//分析过一个ap的分析体积，

                //这样上游浓度如果变化的话，计算结果就会有变化了.........................................................................................
                DownstreamOverallConcentrationTBox.Text = (Filter.OverallPenetration * Convert.ToDouble(UpstreamConcentrationTBox.Text)).ToString();
                DownstreamLocalConcentrationTBox.Text = (Filter.LocalPenetration * Convert.ToDouble(UpstreamConcentrationTBox.Text)).ToString();

                double DownstreamOverallConcentration = Convert.ToDouble(DownstreamOverallConcentrationTBox.Text);
                double DownstreamLocalConcentration = Convert.ToDouble(DownstreamLocalConcentrationTBox.Text);
                double AnalysisVolume = Convert.ToDouble(AnalysisVolumeTBox.Text);

                int ExpectedPNWithoutLeak = Convert.ToInt32(DownstreamOverallConcentration * AnalysisVolume);
                ExpectedPNWithoutLeakTBox.Text = ExpectedPNWithoutLeak.ToString();
                int ExpectedPNWithLeak = Convert.ToInt32(DownstreamLocalConcentration * AnalysisVolume);
                ExpectedPNWithLeakTBox.Text = ExpectedPNWithLeak.ToString();

                GlobalVariables.kb = Convert.ToDouble(LossFactorKbTBox.Text);

                int ExpectedPNWithLeakKb = Convert.ToInt32(ExpectedPNWithLeak * GlobalVariables.kb);
                ExpectedPNWithLeakKbTBox.Text = ExpectedPNWithLeakKb.ToString();

                //下面两句只是写代码的一个参考。。。。。。。。。。。。
                //UpperLimitTBox.Text = (chisquaredistr.invchisquaredistribution((Convert.ToDouble(ParticleTBox.Text) + 1) * 2, 0.025) / 2).ToString();
                //LowerLimitTBox.Text = (chisquaredistr.invchisquaredistribution(Convert.ToDouble(ParticleTBox.Text) * 2, 0.975) / 2).ToString();

                int MaxCountLimit, MinCountLimit;

                MaxCountLimit = Convert.ToInt32(chisquaredistr.invchisquaredistribution((ExpectedPNWithoutLeak + 1) * 2, 0.025) / 2);
                MinCountLimit = Convert.ToInt32(chisquaredistr.invchisquaredistribution(ExpectedPNWithLeakKb * 2, 0.975) / 2);

                MaxCountLimitTBox.Text = MaxCountLimit.ToString();
                MinCountLimitTBox.Text = MinCountLimit.ToString();

                int SignalValue;
                SignalValue = MinCountLimit;

                int SignalDifference;
                SignalDifference = MinCountLimit - MaxCountLimit;

                SignalValueTBox.Text = SignalValue.ToString();
                SignalDifferenceTBox.Text = SignalDifference.ToString();
            }
            catch (System.Exception ex)
            {
                GlobalVariables.log.Error("Error in private void CalculateBtn_Click(object sender, EventArgs e) : " + ex.ToString());
                MessageBox.Show("Error: " + ex.ToString());
            }
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void OKBtn_Click(object sender, EventArgs e)
        {
            Filter.Type = FilterTypeCBox.SelectedItem.ToString();
            GlobalVariables.strMPPS = MPPSCBox.SelectedItem.ToString();

            Filter.Length = Convert.ToUInt32(FrameFilterLengthTBox.Text);
            Filter.Width = Convert.ToUInt32(FrameFilterWidthTBox.Text);
            Filter.Height = Convert.ToUInt32(FrameFilterHeightTBox.Text);

            Filter.FoldingLength = Convert.ToUInt32(FoldingFilterLengthTBox.Text);
            Filter.FoldingWidth = Convert.ToUInt32(FoldingFilterWidthTBox.Text);

            GlobalVariables.DilutionRatio = 1.0 / Convert.ToDouble(DilutionFactorTBox.Text);

            Probe.ap = Convert.ToDouble(Probe_apTBox.Text);
            Probe.bp = Convert.ToDouble(Probe_bpTBox.Text);
            Probe.SettingVelocity = Convert.ToDouble(ScanningVelocityTBox.Text);  // 单位为：mm/s，这个只是设定速度，实际的扫描速度会根据实际的各种参数稍做调整，Probe.ActualVelocity为实际的扫描速度
            
            GlobalVariables.kb = Convert.ToDouble(LossFactorKbTBox.Text);

            //doc.Load(GlobalVariables.RootDirectory + "ApplicationData\\AppConfig.xml");

            (doc.GetElementsByTagName("FilterType")).Item(0).InnerText = Filter.Type;

            (doc.GetElementsByTagName("MPPS")).Item(0).InnerText = Convert.ToString(MPPSCBox.SelectedItem);

            (doc.GetElementsByTagName("FilterLength")).Item(0).InnerText = FrameFilterLengthTBox.Text;
            (doc.GetElementsByTagName("FilterWidth")).Item(0).InnerText = FrameFilterWidthTBox.Text;
            (doc.GetElementsByTagName("FilterHeight")).Item(0).InnerText = FrameFilterHeightTBox.Text;

            (doc.GetElementsByTagName("FoldingLength")).Item(0).InnerText = FoldingFilterLengthTBox.Text;
            (doc.GetElementsByTagName("FoldingWidth")).Item(0).InnerText = FoldingFilterWidthTBox.Text;

            (doc.GetElementsByTagName("UpstreamConcentration")).Item(0).InnerText = UpstreamConcentrationTBox.Text;

            (doc.GetElementsByTagName("Probe_ap")).Item(0).InnerText = Probe_apTBox.Text;
            (doc.GetElementsByTagName("Probe_bp")).Item(0).InnerText = Probe_bpTBox.Text;

            (doc.GetElementsByTagName("SettingVelocity")).Item(0).InnerText = ScanningVelocityTBox.Text;

            (doc.GetElementsByTagName("LossFactorKb")).Item(0).InnerText = LossFactorKbTBox.Text;

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineOnAttributes = true;

            XmlWriter tr = XmlWriter.Create(GlobalVariables.RootDirectory + "ApplicationData\\AppConfig.xml", settings);
            doc.Save(tr);
            tr.Close();

            Close();

        }

        private void CancelBtn_Click_1(object sender, EventArgs e)
        {

        }

    }
}
